package com.lemon.cloud.admin.web;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.lemon.cloud.admin.dto.OrgDTO;
import com.lemon.cloud.admin.service.SysOrgService;
import com.lemon.cloud.comm.constants.enums.LogBusType;
import com.lemon.cloud.comm.model.ResultMsg;
import com.lemon.cloud.comm.po.SysOrg;
import com.lemon.cloud.datascope.utils.DataScopeUtils;
import com.lemon.cloud.log.annotation.SysLog;
import com.lemon.cloud.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("org")
@Tag(name = "部门接口")
public class SysOrgController {

    @Autowired
    private SysOrgService orgService;

    @Operation(
            summary = "获取部门树结构",
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @GetMapping("/tree")
    public ResultMsg<List<Tree<Long>>> getOrgTree() {
        return ResultMsg.resultSuccess(orgService.getOrgTree());
    }

    /**
     * 新增部门
     *
     * @param orgDTO
     * @return
     */
    @Operation(
            summary = "新增部门",
            responses = {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @SysLog(value = "新增部门", businessType = LogBusType.ADD)
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys_org_add')")
    public ResultMsg<SysOrg> addOrg(@Parameter @Valid @RequestBody OrgDTO orgDTO) {
        Assert.state(DataScopeUtils.isInDataScope(Lists.newArrayList(orgDTO.getParentId())), "操作的父级部门不在数据权限内");
        return ResultMsg.resultSuccess(orgService.addOrg(orgDTO));
    }

    /**
     * 查询详情
     *
     * @return
     */
    @Operation(
            summary = "查询详情",
            parameters = @Parameter(name = "id", required = true,description = "部门id",in = ParameterIn.PATH),
            responses= {
            @ApiResponse(responseCode = "200", description = "successful operation")
    }
    )
    @GetMapping("{id:\\d+}")
    public ResultMsg<SysOrg> getOrg(@PathVariable("id") Long id) {
        return ResultMsg.resultSuccess(orgService.getOrg(id));
    }

    /**
     * 删除
     *
     * @return
     */
    @Operation(
            summary = "删除部门",
            parameters = @Parameter(name = "id", required = true,description = "部门id",in = ParameterIn.PATH),
            responses= {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @DeleteMapping("{id:\\d+}")
    @PreAuthorize("hasAnyAuthority('sys_org_del')")
    public ResultMsg<Boolean> delOrg(@PathVariable("id") Long id) {
        Assert.state(DataScopeUtils.isInDataScope(Lists.newArrayList(id)), "操作的部门不在数据权限内");
        return ResultMsg.resultSuccess(orgService.delOrg(id));
    }

    /**
     * 修改部门
     *
     * @param orgDTO
     * @return
     */
    @Operation(
            summary = "修改部门",
            responses= {
                    @ApiResponse(responseCode = "200", description = "successful operation")
            }
    )
    @SysLog(value = "修改部门", businessType = LogBusType.MODIFY)
    @PutMapping
    @PreAuthorize("hasAnyAuthority('sys_org_edit')")
    public ResultMsg<SysOrg> updateOrg(@Parameter @Valid @RequestBody OrgDTO orgDTO) {
        Assert.state(DataScopeUtils.isInDataScope(Lists.newArrayList(orgDTO.getId())), "操作的部门不在数据权限内");
        return ResultMsg.resultSuccess(orgService.updateOrg(orgDTO));
    }


    @GetMapping(value = "/getDescendantList/{orgIds}")
    @Inner
    @Hidden
    ResultMsg<List<SysOrg>> getDescendantList(@PathVariable(value = "orgIds") String orgIds) {
        return ResultMsg.resultSuccess(orgService.getDescendantList(Arrays.asList(orgIds.split(StrUtil.COMMA))));
    }
}
